﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CVS_log_parser;

namespace CVS_log_visualiser
{
    public partial class Form1 : Form
    {
        public string WorkingDirectory { get; set; }

        public event EventHandler WorkingDirectoryChanged;

        CvsFileInfoCollection cvsFiles;

        public CvsFileInfoCollection CvsFiles
        {
            private get
            {
                return this.cvsFiles;
            }

            set
            {
                this.cvsFiles = value;

                UpdateData( this.groupingFunctors[this.listViewDate], this.listViewDate );
                //UpdateData( f => f.ParentFileInfo.WorkingFileName, this.listViewWorkingFileName );
                //UpdateData( f => f.Revision, this.listViewRevision );
                //UpdateData( f => f.Author, this.listViewAuthor );
                //UpdateData( f => f.FileName, this.listViewFileName );
                //UpdateData( f => f.Comment, this.listViewComment );
            }
        }

        void UpdateData( Func<CvsFileRevision, object> selector, ListView listView )
        {
            //this.setWorkingdirectoryToolStripMenuItem.Enabled = false;
            listView.Visible = false;

            //this.BackgroundDataUpdater.RunWorkerAsync( Tuple.Create( listView, selector ) );
            new ListViewFiller( listView, this.CvsFiles ).UpdateData( selector );
        }

        void OnWorkingDirectoryChanged()
        {
            EventHandler evt = this.WorkingDirectoryChanged;

            if ( evt != null )
                evt( this, EventArgs.Empty );
        }

        Dictionary<ListView, Func<CvsFileRevision, object>> groupingFunctors = new Dictionary<ListView,Func<CvsFileRevision,object>>();

        public Form1()
        {
            InitializeComponent();

            this.Controls.OfType<ListView>().ToList().ForEach( listView => listView.ListViewItemSorter = new ListViewColumnSorter<ListViewItem>() );

            groupingFunctors.Add( this.listViewWorkingFileName, new Func<CvsFileRevision, object>( f => f.ParentFileInfo.WorkingFileName ) );
            groupingFunctors.Add( this.listViewRevision, new Func<CvsFileRevision, object>( f => f.Revision ) );
            groupingFunctors.Add( this.listViewDate, new Func<CvsFileRevision, object>( f => f.Date.Date ) );
            groupingFunctors.Add( this.listViewAuthor, new Func<CvsFileRevision, object>( f => f.Author ) );
            groupingFunctors.Add( this.listViewFileName, new Func<CvsFileRevision, object>( f => f.FileName ) );
            groupingFunctors.Add( this.listViewComment, new Func<CvsFileRevision, object>( f => f.Comment ) );
            //this.BackgroundDataUpdater.DoWork += new DoWorkEventHandler( BackgroundDataUpdater_DoWork );
            //this.BackgroundDataUpdater.RunWorkerCompleted += new RunWorkerCompletedEventHandler( BackgroundDataUpdater_RunWorkerCompleted );
            //this.BackgroundDataUpdater.ProgressChanged += new ProgressChangedEventHandler( BackgroundDataUpdater_ProgressChanged );
        }

        //void BackgroundDataUpdater_ProgressChanged( object sender, ProgressChangedEventArgs e )
        //{
        //    this.toolStripProgressBar.PerformStep();
        //}

        //void BackgroundDataUpdater_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
        //{
        //    this.listViewDate.Visible =
        //    this.groupByToolStripMenuItem.Enabled = true;
        //    this.toolStripProgressBar.ProgressBar.Value = 0;
        //}

        //void BackgroundDataUpdater_DoWork( object sender, DoWorkEventArgs e )
        //{
        //    //UpdateDataInBackground( e.Argument as Func<CvsFileRevision, object> );
        //    var args = e.Argument as Tuple<ListView, Func<CvsFileRevision, object> >;

        //    new ListViewFiller() { ListView = args.Item1, CvsFiles = CvsFiles.ToArray() }.UpdateDataInBackground( args.Item2 );
        //}

        private void exitToolStripMenuItem_Click( object sender, EventArgs e )
        {
            Close();
        }

        private void setWorkingdirectoryToolStripMenuItem_Click( object sender, EventArgs e )
        {
            if ( this.folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK )
            {
                this.WorkingDirectory = this.toolStripStatusLabel1.Text = this.folderBrowserDialog.SelectedPath;

                OnWorkingDirectoryChanged();
            }
        }

        //BackgroundWorker BackgroundDataUpdater = new BackgroundWorker() { WorkerReportsProgress = true };

        private void listViewMain_ColumnClick( object sender, ColumnClickEventArgs e )
        {
            var lv = sender as ListView;

            if ( lv == null )
                return;

            var lvwColumnSorter = lv.ListViewItemSorter as ListViewColumnSorter<ListViewItem>;

            // Determine if clicked column is already the column that is being sorted.
            if ( e.Column == lvwColumnSorter.SortColumn )
            {
                // Reverse the current sort direction for this column.
                if ( lvwColumnSorter.Order == SortOrder.Ascending )
                {
                    lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }

            // Perform the sort with these new sort options.
            lv.Sort();
        }

        private void Form1_Load( object sender, EventArgs e )
        {
            this.Show();
            this.setWorkingdirectoryToolStripMenuItem_Click( this, EventArgs.Empty );
        }

        private void listViewMain_MouseUp( object sender, MouseEventArgs e )
        {
            var child = this.listViewDate.GetChildAtPoint( e.Location );
        }

        private void tabControl1_Selected( object sender, TabControlEventArgs e )
        {
            var tabCtrl = sender as TabControl;
            var listView = tabCtrl.SelectedTab.Controls.OfType<ListView>().FirstOrDefault();

            if ( listView == null || listView.Items.Count != 0 )
                return;

            new ListViewFiller( listView, this.CvsFiles ).UpdateData( this.groupingFunctors[listView] );
        }
    }
}
